<?php

namespace App\Api\Logic;

use App\Api\Logic\Validate\TransferMoneyValidate;
use App\Exceptions\ValidationException;
use App\Helpers\ResponseEnum;
use App\Models\TongTransferMoney;
use App\Models\TongUser;

class TransferMoneyLogic extends BaseLogic
{

    /**
     * 获取验证的实力
     * @return TransferMoneyValidate
     */
    private function getValidate():TransferMoneyValidate{
        return  new TransferMoneyValidate();
    }


    /**
     * 用户转账
     * @param array $data
     * @return void
     */
    public function to_user_money(array $data)
    {
        $validate = $this->getValidate();
        if(!$validate->scene('to_user_money')->check($data)){
            $this->error($validate->getError());
        }
        //throw new ValidationException(ResponseEnum::HTTP_ERROR,'系统升级，请先核对自己的数据');
        //获取接受人的id
        $user_info = TongUser::query()->where('id',$data['u_id'])->first();
        if($user_info['money']<$data['money']){
            throw new ValidationException(ResponseEnum::HTTP_ERROR,'金额不足');
        }
        $to_query = TongUser::query()->where('mobile',$data['to_mobile']);
        if(!$to_query->exists()){
            throw new ValidationException(ResponseEnum::HTTP_ERROR,'没有找到此人');
        }
    }

    /**
     * 获取转账的详情
     * @param array $data
     * @return void
     */
    public function getToUserMoneyInfo(array $data)
    {
        $validate = $this->getValidate();
        if(!$validate->scene('getToUserMoneyInfo')->check($data)){
            $this->error($validate->getError());
        }
        $query = TongTransferMoney::query()->where('id',$data['id'])->where('u_id',$data['u_id']);
        if(!$query->exists()){
            throw new ValidationException(ResponseEnum::HTTP_ERROR,'数据参数有错误');
        }
    }

    /**
     * @param array $data
     * @return void
     */
    public function confirmUserMoney(array $data)
    {
        $validate = $this->getValidate();
        if(!$validate->scene('confirmUserMoney')->check($data)){
            $this->error($validate->getError());
        }
        $query = TongTransferMoney::query()->where('id',$data['id'])->where('u_id',$data['u_id'])->where('status',1);
        if(!$query->exists()){
            throw new ValidationException(ResponseEnum::HTTP_ERROR,'数据参数有错误');
        }
        //获取用户详情
        $user_info = TongUser::query()->where('id',$data['u_id'])->first();
        $info = $query->first();
        if($user_info['money']<$info['money']){
            throw new ValidationException(ResponseEnum::HTTP_ERROR,'找好余额不足');
        }
    }
}
